package com.apps.ftapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.apps.ftapp.R;

import com.apps.ftapp.adapter.MyLessonCacheJyAdapter;
import com.apps.ftapp.db.Kj;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.SDCardUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyLessonCacheJyActivity extends BaseActivity {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private List dataList = new ArrayList();
    private MyLessonCacheJyAdapter myLessonCacheAdapter;


    List<Kj> lessonList = new ArrayList<>();


    private String dirName = SDCardUtils.getSDCardPath() + "duobeiyun/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_lesson_cache_jy);
        ButterKnife.bind(this);


        initView();


        setTitle("缓存讲义");


        loadData();

    }

    private void loadData() {

        lessonList = DataSupport.findAll(Kj.class);
        if (null != lessonList && lessonList.size() > 0) {
            myLessonCacheAdapter.addRes(lessonList);
        }

    }

    private void initView() {

        myLessonCacheAdapter = new MyLessonCacheJyAdapter(getApplicationContext(), dataList);
        listView.setAdapter(myLessonCacheAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                L.i("main", "====" + dirName + lessonList.get(i).getLessonid() + ".pdf");

//                File file1 = new File(dirName);
                //  sdcard/test1.pdf为本地pdf文件的路径
//                Intent intent = OpenFiles.getPdfFileIntent(dirName + lessonList.get(i).getLessonid() + ".pdf");
//                startActivity(intent);
                String uri = dirName + lessonList.get(i).getLessonid() + ".pdf";
                Intent intent = new Intent();
                intent.setClass(MyLessonCacheJyActivity.this, PdfActivity.class);
                intent.putExtra("uri", uri);
                startActivity(intent);
//                ActivitySkipUtil.skipAnotherActivity(MyLessonCacheJyActivity.this,MyLessonCacheDetailActivity.class,false);
            }
        });
    }

}
